/*
 * @Author: EltonLi lijiaming@uavi-tech.com
 * @Date: 2024-03-29 11:56:06
 * @LastEditors: lvgrape lvgrape@outlook.com
 * @LastEditTime: 2024-05-15 15:23:41
 * @FilePath: \PREMIUM_RTThread_V1.0\ZINO\hardware\rgb\rgb_tmr.c
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#include <rtthread.h>
#include <rtdevice.h>
#include "rgb_tmr.h"
#include "zino.h"

#define DBG_TAG "rgb_tmr"
#define DBG_LVL DBG_LOG
#include <rtdbg.h>

#define HWTIMER_DEV_NAME "timer11"

#define PIN_R 11 // PA11
#define PIN_G 12 // PA12
#define PIN_B 13 // PA13
rgb_tmr_t rgb;
void rgb_tmr_run()
{
    if (rgb.r || rgb.g || rgb.b)
    {
        rgb.c++;

        if (rgb.c > 255)
        {
            rgb.c = 0;
            rt_pin_write(11, PIN_LOW);
            rt_pin_write(12, PIN_LOW);
            rt_pin_write(13, PIN_LOW);
        }
        if (rgb.c > rgb.r)
        {
            rt_pin_write(13, PIN_HIGH);
        }
        if (rgb.c > rgb.g)
        {
            rt_pin_write(12, PIN_HIGH);
        }
        if (rgb.c > rgb.b)
        {
            rt_pin_write(11, PIN_HIGH);
        }
    }
    else
    {
        rt_pin_write(11, PIN_HIGH);
        rt_pin_write(12, PIN_HIGH);
        rt_pin_write(13, PIN_HIGH);
    }
}

static rt_err_t timeout_cb(rt_device_t dev, rt_size_t size)
{
    rgb_tmr_run();
    pwrLedHandle();
    // rt_kprintf("this is hwtimer timeout callback fucntion!\n");
    // rt_kprintf("tick is :%d !\n", rt_tick_get());
    return 0;
}

void rbg_led_set(uint8_t r, uint8_t g, uint8_t b)
{
    rgb.r = r;
    rgb.g = g;
    rgb.b = b;
    // LOG_D("r:%d g:%d b:%d c:%d\n", r, g, b, rgb.c);
}

static int hwtimer_rgb()
{
    rt_err_t ret = RT_EOK;
    rt_hwtimerval_t timeout_s;    /* 定时器超时值 */
    rt_device_t hw_dev = RT_NULL; /* 定时器设备句柄 */
    rt_hwtimer_mode_t mode;       /* 定时器模式 */
    rt_uint32_t freq = 1000000;   /* 计数频率 */
    
    // gpio_pin_remap_config(SWJTAG_GMUX_100,TRUE);
    rt_pin_mode(11, PIN_MODE_OUTPUT_OD);
    rt_pin_mode(12, PIN_MODE_OUTPUT_OD);
    rt_pin_mode(13, PIN_MODE_OUTPUT_OD);
    
    hw_dev = rt_device_find(HWTIMER_DEV_NAME);
    if (hw_dev == RT_NULL)
    {
        rt_kprintf("hwtimer sample run failed! can't find %s device!\n", HWTIMER_DEV_NAME);
        return RT_ERROR;
    }

    /* 以读写方式打开设备 */
    ret = rt_device_open(hw_dev, RT_DEVICE_OFLAG_RDWR);
    if (ret != RT_EOK)
    {
        rt_kprintf("open %s device failed!\n", HWTIMER_DEV_NAME);
        return ret;
    }

    /* 设置超时回调函数 */
    rt_device_set_rx_indicate(hw_dev, timeout_cb);

    /* 设置计数频率(若未设置该项，默认为1Mhz 或 支持的最小计数频率) */
    rt_device_control(hw_dev, HWTIMER_CTRL_FREQ_SET, &freq);
    /* 设置模式为周期性定时器（若未设置，默认是HWTIMER_MODE_ONESHOT）*/
    mode = HWTIMER_MODE_PERIOD;
    ret = rt_device_control(hw_dev, HWTIMER_CTRL_MODE_SET, &mode);
    if (ret != RT_EOK)
    {
        rt_kprintf("set mode failed! ret is :%d\n", ret);
        return ret;
    }

    /* 设置定时器超时值为5s并启动定时器 */
    timeout_s.sec = 0;   /* 秒 */
    timeout_s.usec = 66; /* 微秒 */
    if (rt_device_write(hw_dev, 0, &timeout_s, sizeof(timeout_s)) != sizeof(timeout_s))
    {
        rt_kprintf("set timeout value failed\n");
        return RT_ERROR;
    }

    /* 延时3500ms */
    // rt_thread_mdelay(3500);

    /* 读取定时器当前值 */
    rt_device_read(hw_dev, 0, &timeout_s, sizeof(timeout_s));
    // rt_kprintf("Read: Sec = %d, Usec = %d\n", timeout_s.sec, timeout_s.usec);

    return ret;
}

long rgb_set(int argc, char **argv)
{
    if (argc == 5)
    {
        int r, g, b;
        // i = atoi(argv[1]);
        r = atoi(argv[1]);
        g = atoi(argv[2]);
        b = atoi(argv[3]);
        if (r >= 0 && r <= 255 && g >= 0 && g <= 255 && b >= 0 && b <= 255)
        {
            // zino_pwr_ctrl(PWR_CTRL_5V_ON);
            rbg_led_set(r, g, b);
            rt_kprintf("rbg_led_set %d %d %d\n", r, g, b);
            // rgb_show();
        }
    }
    else
    {
        rt_kprintf("Usage:\n");
        rt_kprintf("\trgb_set 255 255 255\n");
    }
    return RT_EOK;
}



// ZINO_BOARD_EXPORT(at32_tmr_pwm_dma_init);
MSH_CMD_EXPORT(rgb_set, rgb set);

ZINO_APP_EXPORT(hwtimer_rgb);
